package com.AEWMS.Servlet;

import com.AEWMS.Dao.Admin;
import com.AEWMS.Dao.Parent;
import com.AEWMS.Dao.Student;
import com.AEWMS.Pojo.StudentInfoView;
import com.AEWMS.Service.AdminService;
import com.AEWMS.Service.ParentService;
import com.AEWMS.Service.StudentService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "logonServlet", urlPatterns = "/logonServlet")
public class logonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("p1");
        String passWord = request.getParameter("p2");
        String userType = request.getParameter("p3");
        System.out.println(userName+passWord+userType);
            //学生登录
        if(userType.equals("student")){
            StudentService ss = new StudentService();
            Student s = ss.findStuNotViewById(userName);
                if (s.getPassword() != null && s.getPassword().equals(passWord)){
                    StudentInfoView sv = ss.findStuById(userName);
                    sv.setType(s.getType());
                    response.getWriter().println(JSON.toJSONString(sv));
                } else {
                    response.getWriter().println(JSON.toJSONString("用户名或密码错误"));
                }
            ss.closeCon();
            return;
        }
        //管理员老师登录
        if (userType.equals("admin")){
            AdminService as = new AdminService();
            Admin a = as.findAdmById(userName);
            if(a.getAdminPassword() != null && a.getAdminPassword().equals(passWord) ){
                response.getWriter().println(JSON.toJSONString(a));
            }else {
                response.getWriter().println(JSON.toJSONString("用户名或密码错误"));
            }
            as.closeCon();
            return;
        }
        //家长登录
        if (userType.equals("parent")){
            ParentService ps = new ParentService();
            Parent p = ps.findChildById(userName);
            if(p.getParentPassword() != null && p.getParentPassword().equals(passWord)){
                response.getWriter().println(JSON.toJSONString(p));
            }else{
                response.getWriter().println(JSON.toJSONString("用户名或密码错误"));
            }
            ps.closeCon();
        }
    }
}